package manager.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import manager.bean.Department;
import manager.bean.LayData;
import manager.dao.DepartmentDao;
import manager.util.JDBC;
import manager.util.SYS;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@WebServlet("/department/*")
public class DepartmentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object path = req.getAttribute(SYS.REQUEST_PATH);

        System.out.println(path.toString());
        if ("list".equals(path)){
            List list = list(req);
            int count = count(req);
            LayData layData = new LayData(count,list);
            //用字符串来表示一个对象  JSON格式
            String json = JSON.toJSONString(layData,SerializerFeature.WriteDateUseDateFormat);
            resp.getWriter().write(json);
            System.out.println(json);
        }else if ("add".equals(path)){
            //新增

            System.out.println(path);
            int a = add(req);
            if (a==1){
                resp.getWriter().write("true");
            }else {
                resp.getWriter().write("false");
            }
        }else if ("edit".equals(path)){
            //修改
            System.out.println(path);
            int a = edit(req);
            if (a==1){
                resp.getWriter().write("true");
            }else {
                resp.getWriter().write("false");
            }
        }else if ("delete".equals(path)){
            //删除
            System.out.println(path);
            delete(req);
        }else if ("all".equals(path)){
            List list = all(req);
            String json = JSON.toJSONString(list);
            resp.getWriter().write(json);
        }else if ("initstafflist".equals(path)){
            List list = initDepartmentList(req);
            String json = JSON.toJSONString(list);
            resp.getWriter().write(json);
        }
        else {
            //无效的
        }

    }
    private DepartmentDao dao = new DepartmentDao();

    private List all(HttpServletRequest request){
        List list = null;
        String sql = "select name,id from department";
        List list1 = dao.getDepartment(sql);
//        if (list1.size()!=0){
//            for (int i = 0; i <list1.size() ; i++) {
//            System.out.println(list1.get(i));
//            List department = (List) list1.get(i);
//            }
//        }
        return list1;
    }

    private List list(HttpServletRequest request){
        List list = null;
        String checktext = request.getParameter("checktext");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int start = (page - 1) * limit;
        String sql = "select * from department ";
        if (checktext!=null){
            sql += " where name like ?  limit ?,?";
            System.out.println(sql+"***");
            list = dao.getDepartment(sql,"%"+checktext+"%",start,limit);
        }else {
            sql += "limit ?,? ";
//            System.out.println(sql);
            list = dao.getDepartment(sql,start,limit);
        }
        return list;
    }
    private void delete(HttpServletRequest request){
        String str = request.getParameter("id");
        System.out.println(str);
        if (str!=null) {
            String sql = "delete from department where id = '" + str + "'";
            System.out.println(sql);
            dao.deleteDepartment(sql);
        }
    }
    private int edit(HttpServletRequest request){
        String str = request.getParameter("name");
        String id = request.getParameter("id");
        System.out.println(str);
        System.out.println(id);
        String sql = "update department set name = '"+str+"' where id = "+id;
        System.out.println(sql);
        int count = dao.editDepartment(sql);
        return count;
    }
    private int add(HttpServletRequest request){

        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String count = request.getParameter("count");
        String code = request.getParameter("code");
        System.out.println(id+name+count);
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String sql = "insert department values("+id+",'"+name+"',"+count+",'"+time+"',"+code+")";
        System.out.println(sql);
        return dao.addDepartment(sql);
    }

    private int count(HttpServletRequest request){
        String checktext = request.getParameter("checktext");
        int count = 0;
        String sql = "select count(*) from department";
        if (checktext!=null){
            sql += "where name like ? ";
            count = dao.countDepartment(sql,"%"+checktext+"%");
        }else {
            count = dao.countDepartment(sql);
        }
        return count;
    }
    private List initDepartmentList(HttpServletRequest request){
        String sql = "select name,id,count from department";
        return dao.getDepartment(sql);
    }

}
